<script>
	import { mapState } from 'vuex'
	import { stringify } from 'qs'
	import store from '../store/index.js'
	export default {
		data() {
			return {
				storageMobile: ''
			}
		},
		computed: {
			...mapState({
				token: state => state.token,
				userInfo: state => state.userInfo
			}),
		},
		watch: {
			token(val, oldVal) {
				if (val && !oldVal) {
					this.getUser()
				}
			}
		},
		mounted() {
			this.getUser()
		},
		methods: {
			getUser() {
				if (this.token) {
					// this.$getUserInfo().then(() => {
					// 	this.storageMobile = this.$getStorage('myShare_userInfo') && this.$getStorage('myShare_userInfo').mobile;
					// 	 store.commit('freshActivite', true); 
					// })
				}
			},
			getUserInfo(e, type, subType, id, sid, isCheckBirth = 0) {
				const { detail } = e
				console.log(detail, 'detail');
				this.storageMobile = this.$getStorage('myShare_userInfo') && this.$getStorage('myShare_userInfo').mobile;
				console.log(this.token, this.storageMobile ,this.userInfo, type);
 				if (this.token  && this.userInfo &&  this.storageMobile) {
					if (type === 1) { // type：1（我的记录）；2（详情页分享）；3（详情页免费领取）
						this.$goPage(1, '/pages/personalPage/index')
					}
				} else {
					console.log(2);
					const _this = this
					if (detail && detail.errMsg === 'getUserInfo:ok') {
						_this.$getUniLogin(detail.userInfo, id, sid).then((res) => {
							_this.$setStorage('myShare_wxUserInfo',  detail.userInfo);
							let perfectToUrl = subType === 1 ? `/pages/index/index?${stringify(this.$getQuery())}` : `/pages/actDetail/actDetail?${stringify(this.$getQuery())}`
							_this.$setStorage('myShare_perfectToUrl', perfectToUrl)
							_this.$getUserInfo().then(() => {
								let userInfo = this.$getStorage('myShare_userInfo') ;
								_this.storageMobile = userInfo.mobile;
								console.log(userInfo);
								if(type === 4) {
									console.log(5)
									uni.navigateBack();
									return;
								}
								
								if(!_this.storageMobile) {
									console.log(4)
									_this.$goPage(1, `/pages/login/index?source=${id}&utm=${sid}&clickType=${type}`)
								} else if(_this.storageMobile &&isCheckBirth === 1 &&  userInfo.type === 1 && (!userInfo.address || !userInfo.birthday)){
									console.log(5);
									_this.$goPage(1, '../login/perfect')
							         _this.$setStorage('loginShow', false);
							         store.commit('loginShow', false); // 隐藏我的记录
								}
							})	
						})
					}
				}
			}
		}
	}
</script>
